<?php
class Add extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/rbac/node/add'));
		
		$data = array();
		$tpl = 'rbac/role/roleinfo.html';
		
		$this->display($data, $tpl);
	}
	public function json() {
		$name = $this->post('name', 'str');
		
		if(!$name) $this->displayJSON(array('status' => -1, 'message' => '请输入角色名称'));
		
		$mRole = new Role_model();
		//检查角色名称
		$params = array('name' => $name);
		$result = $mRole->roles($params);
		if($result) $this->displayJSON(array('status' => -3, 'message' => '角色已存在'));
		
		$cols = array('name' => $name);
		$result = $mRole->createRole($cols);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '添加角色成功'));
		else $this->displayJSON(array('status' => -4, 'message' => '添加角色失败'));
	}
}